import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useState, useRef, useEffect, useImperativeHandle, forwardRef, useCallback, } from "react";
import DevSelects from "../../../../components-bs/components/yearSelect";
import { message, Select, TreeSelect } from "antd";
import { SyncOutlined } from "@ant-design/icons";
import DevGrid, { DevGridSearchIpt, } from "~/public/cpas-ui/components-ui/express-components/DevGrid";
import CpasTable from "../../HandSonGrid";
import notify from "devextreme/ui/notify";
import { getDisclosureData, getAnnotatedDataApi, getCpasTableList, getAllDesignTableApi, getCheckTypeApi, } from "../../api/DesignTableApi";
import { getYears, getDataId, formatPrice, sortCpasTable, } from "../../../../tools/Tools";
import { dynamicFormulaReplace, incellDynamicFormulaReplace, } from "../../classes/DynamicFormula";
import { debouncedApi } from "./tools";
// 报表
export const TableBase = forwardRef((props, ref) => {
    const { sourceOrCheck, gridDatas, data, dataList, setDataList, parentType, parentName, parentParamsRef, } = props;
    const cpasTableRef = useRef(null);
    const [getVerifiedFormula, setVerifiedFormula] = useState([]);
    const [getVerifyFormula, setVerifyFormula] = useState([]);
    const debouncedApiCall = useCallback(debouncedApi, [dataList]);
    const onCellClick = (item, c, f, name) => {
        const textType = cpasTableRef.current.hot.getCellMeta(c.row, c.col);
        const dynamicCondition = data && data?.dynamicCondition ? true : false;
        if (textType.type === "text") {
            notify("表内公式设置必须为数字", "error", 2000);
        }
        else {
            if (sourceOrCheck === "check") {
                const dynamicVal = incellDynamicFormulaReplace(dynamicCondition, parentType, parentName, name.rowMap, name.columnMap, textType, c.col);
                const val = dynamicVal
                    ? dynamicVal
                    : `IN_CELL("${name.rowMap}","${name.columnMap}")`;
                getVerifiedFormula.push(val);
                setVerifiedFormula([...getVerifiedFormula]);
                const newVal = data.beVerifiedFormula && !dynamicVal
                    ? data.beVerifiedFormula + "+" + getVerifiedFormula.join("+")
                    : getVerifiedFormula.join("+");
                data.beVerifiedFormula = newVal;
                setVerifiedFormula([]);
                setDataList([...dataList]);
                debouncedApiCall(newVal, data, "beVerifyFormat", "beVerifiedFormula", parentParamsRef, dataList, setDataList);
            }
            cpasTableRef.current.reRender();
        }
        if (sourceOrCheck === "source") {
            if (textType.type === "text") {
                notify("表内公式设置必须为数字", "error", 2000);
            }
            else {
                // const isDynamic = getDynamic("ds", textType);
                // console.log(isDynamic, 'isDynamic---source');
                const dynamicVal = incellDynamicFormulaReplace(dynamicCondition, parentType, parentName, name.rowMap, name.columnMap, textType, c.col);
                console.log(dynamicVal, "dynamicVal----");
                const val = dynamicVal
                    ? dynamicVal
                    : `IN_CELL("${name.rowMap}","${name.columnMap}")`;
                getVerifyFormula.push(val);
                setVerifyFormula([...getVerifyFormula]);
                const newVal = data.verifyFormula
                    ? data.verifyFormula + "+" + getVerifyFormula.join("+")
                    : getVerifyFormula.join("+");
                data.verifyFormula = newVal;
                setVerifyFormula([]);
                setDataList([...dataList]);
                debouncedApiCall(newVal, data, "verifyFormat", "verifyFormula", parentParamsRef, dataList, setDataList);
            }
        }
    };
    useEffect(() => {
        if (cpasTableRef?.current?.hot) {
            setTimeout(() => {
                cpasTableRef?.current?.hot.render();
            }, 1000);
        }
    }, [gridDatas]);
    useImperativeHandle(ref, () => ({
        contentA: () => {
            setVerifiedFormula([]);
        },
        contentB: () => {
            setVerifyFormula([]);
        },
        cpasTableRef,
    }));
    return (_jsx(React.Fragment, { children: _jsx("div", { className: "cpas-tables", children: _jsx(CpasTable, { className: "cpas-list", numberZeroReplace: " ", mapRowsList: [], mapColumnsList: [], tabGroup: "source-tab", tabLevel: 1, isTest: true, model: "read", isShowMenuToolBar: false, gridData: gridDatas, ref: cpasTableRef, type: parentType, dbCellClick: onCellClick, name: "innerTable" }) }) }));
});
// 报表基础数据
export const TableReport = forwardRef((props, ref) => {
    const { sourceOrCheck, data, dataList, setDataList, testColumns, keys, parentParamsRef, } = props;
    const [baseList, setBaseList] = useState([]);
    const [year, setYear] = useState("本年");
    const devGridRef = useRef(null);
    const iptRef = useRef(null);
    const [dataRef, setDataRef] = useState({});
    const [value, setValue] = useState();
    const [getVerifyFormula, setVerifyFormula] = useState([]);
    const debouncedApiCall = useCallback(debouncedApi, [dataList]);
    const onCellClick = (item) => {
        console.log(item, "item----");
        const dynamicCondition = data && data?.dynamicCondition ? true : false;
        if (sourceOrCheck === "source") {
            const dynamicVal = dynamicFormulaReplace({
                dynamicCondition,
                dynamicType: "year",
                paramType: "default",
                type: "rs",
                formulaData: {
                    tname: "base.reportItems",
                    rowVal: item.data["主键"],
                    colVal: item.column.caption,
                    year,
                },
                rowData: {
                    report: item?.data["所属报表"],
                    caption: item?.column["caption"],
                },
            });
            console.log(dynamicVal, "dynamicVal-----");
            const val = dynamicVal
                ? dynamicVal
                : `FS_CELL("base.reportItems","${item.data["主键"]}","${item.column.caption}","${year}")`;
            getVerifyFormula.push(val);
            setVerifyFormula([...getVerifyFormula]);
            const newVal = data.verifyFormula
                ? data.verifyFormula + "+" + getVerifyFormula.join("+")
                : getVerifyFormula.join("+");
            data.verifyFormula = newVal;
            setVerifyFormula([]);
            setDataList([...dataList]);
            debouncedApiCall(newVal, data, "verifyFormat", "verifyFormula", parentParamsRef, dataList, setDataList);
        }
        else {
            return message.warning("请选择数据源");
        }
    };
    const fetchCheckBaseData = async (years) => {
        const params = {
            name: "base.reportItems",
            data_id: years,
            fields: [],
            where: "",
            order: "",
        };
        const data = await getDisclosureData(params);
        if (data.ok) {
            data.data.map((item) => {
                Object.keys(item).map((t) => {
                    if (typeof item[t] === "number") {
                        item[t] = formatPrice(item[t].toFixed(2));
                    }
                });
            });
            return setBaseList(data.data);
        }
        else {
            return message.error("数据加载失败");
        }
    };
    // 重新加载当前数据
    useImperativeHandle(ref, () => ({
        valueFun: () => {
            setValue(null);
            const curr = dataRef?.gridRef;
            curr.current.instance.searchByText();
        },
    }));
    useEffect(() => {
        // 初始化加载数据
        reloadTable();
        setDataRef(devGridRef.current);
        // dataRef.gridRef.current.instance.searchByText()
    }, [keys]);
    const reloadTable = async () => {
        const data = await getYears(year);
        fetchCheckBaseData(data + "");
    };
    const resetReload = () => {
        setValue(null);
        const curr = dataRef.gridRef.current;
        curr.instance.searchByText();
    };
    return (_jsxs(React.Fragment, { children: [_jsxs("div", { className: "title", children: [_jsxs("div", { className: "title-left ", children: [_jsx("label", { children: " \u5E74\u5EA6: " }), _jsx(DevSelects, { onValueChanged: async (val) => {
                                    const data = await getYears(val.value);
                                    setYear(val.value);
                                    fetchCheckBaseData(data + "");
                                }, yearVals: year, placeholder: "\u8BF7\u9009\u62E9\u5E74\u5EA6" })] }), _jsxs("div", { className: "title-right", style: {
                            display: "flex",
                        }, children: [_jsx("label", { style: { height: "30px", lineHeight: "30px", marginRight: "10px" }, children: "\u9879\u76EE\u540D\u79F0:" }), _jsx(DevGridSearchIpt, { placeholder: "\u8BF7\u8F93\u5165\u9879\u76EE\u540D\u79F0", width: 300, ref: iptRef, devGridRefs: dataRef, value: value, onChange: (val) => {
                                    setValue(val.event.target.value);
                                } }), _jsx(SyncOutlined, { className: "title-right-icon", onClick: resetReload })] })] }), _jsx("div", { className: "cpas-tables report-tables", children: _jsx(DevGrid.Default, { ref: devGridRef, dataSource: baseList, defaultColumns: testColumns, keyExpr: "\u4E3B\u952E", showBorders: true, showColumnLines: true, showRowLines: true, onCellDblClick: (e) => {
                        onCellClick(e);
                    } }) })] }));
});
// 附注数据
export const TableBook = forwardRef((props, ref) => {
    const { sourceOrCheck, focusItem, dataList, setDataList, gridDatas, parentType, parentParamsRef, } = props;
    const cpasTableRef = useRef(null);
    const [value, setValue] = useState();
    const [treeData, setTreeData] = useState([]);
    const [resultData, setResultData] = useState(null);
    const [year, setYear] = useState("本年");
    const [treeItem, setTreeItem] = useState({});
    const [source, setSourceName] = useState("zl");
    const [treeDataAll, setTreeDataAll] = useState([]);
    const [getVerifyFormula, setVerifyFormula] = useState([]);
    const dataOptions = [
        { label: "整理后附注", value: "zl" },
        { label: "整理前披露表", value: "" },
    ];
    const debouncedApiCall = useCallback(debouncedApi, [dataList]);
    useImperativeHandle(ref, () => ({
        valueFun: () => {
            //setValue(null);
            const current = cpasTableRef.current;
            current.reRender();
            //current?.resetTable("ds");
        },
        cpasTableRef,
    }));
    // 单元格双击
    const onCellClick = (item, c, f, name) => {
        const sourceName = source ? "后" : "前";
        if (sourceOrCheck === "source") {
            const textType = cpasTableRef.current.hot.getCellMeta(c.row, c.col);
            const dynamicCondition = focusItem && focusItem?.dynamicCondition ? true : false;
            const dynamicVal = dynamicFormulaReplace({
                dynamicCondition,
                dynamicType: "column",
                paramType: "default",
                type: "ds",
                formulaData: {
                    tname: `${value}|${sourceName}`,
                    rowVal: name.rowMap,
                    colVal: name.columnMap,
                    year: year,
                },
                cellMeta: textType,
            });
            const val = dynamicVal
                ? dynamicVal
                : `CP_CELL("${value}|${sourceName}","${name.rowMap}","${name.columnMap}","${year}")`;
            getVerifyFormula.push(val);
            setVerifyFormula([...getVerifyFormula]);
            const newVal = focusItem.verifyFormula
                ? focusItem.verifyFormula + "+" + getVerifyFormula.join("+")
                : getVerifyFormula.join("+");
            focusItem.verifyFormula = newVal;
            setVerifyFormula([]);
            setDataList([...dataList]);
            debouncedApiCall(newVal, focusItem, "verifyFormat", "verifyFormula", parentParamsRef, dataList, setDataList);
        }
        else {
            return message.error("请选择数据源");
        }
    };
    // 披露表数据
    const fetchAnnotatedData = async (yearVal) => {
        const data = await getAnnotatedDataApi(yearVal);
        let newData = data.filter((item) => {
            item.value = item.name;
            item.label = item.name;
            item.children = data.filter((e) => {
                e.value = e.name;
                e.label = e.name;
                return item.id === e.pid;
            });
            return !item.pid;
        });
        setTreeDataAll(newData);
        let noteid;
        // let newTreeData;
        // if (gridDatas.props.type !== "fs") {
        //   noteid = gridDatas.props.notes.tid;
        //   newTreeData = newData.filter((item) => item.id !== noteid);
        // } else {
        //   newTreeData = newData;
        // }
        return setTreeData(newData);
        // let result = [];
        // if (!Array.isArray(data)) {
        //   return result;
        // }
        // data.forEach((item) => {
        //   delete item.children;
        // });
        // let map = {};
        // data.forEach((item) => {
        //   map[item.id] = item;
        // });
        // data.forEach((item) => {
        //   let parent = map[item.pid];
        //   if (parent) {
        //     (parent.children || (parent.children = [])).push({
        //       ...item,
        //       value: item.title,
        //     });
        //   } else {
        //     result.push({
        //       ...item,
        //       value: item.title,
        //     });
        //   }
        // });
        // return setTreeData(result);
    };
    const initData = async () => {
        const dataId = await getDataId();
        fetchAnnotatedData(dataId);
    };
    const initDataVal = async (years) => {
        const data = await getYears(years);
        fetchAnnotatedData(data);
    };
    useEffect(() => {
        setTimeout(() => {
            const current = cpasTableRef.current;
            current.reRender();
        }, 50);
        initData();
    }, []);
    const deepFun = (data, name) => {
        for (var i = 0; i < data.length; i++) {
            if (data[i].title === name) {
                return data[i];
            }
            else {
                if (data[i].children) {
                    let val = deepFun(data[i].children, name);
                    if (val) {
                        return val;
                    }
                }
            }
        }
    };
    // 披露表下拉选
    const onChange = async (newValue) => {
        const noteid = gridDatas.props.notes.tid;
        if (!newValue) {
            const currentData = treeData.filter((item) => item.id === noteid); //当前表数据
            if (currentData &&
                currentData.length &&
                currentData[0]?.title === newValue) {
                message.warning("当前选择的表数据重复,请重新选择~");
            }
            setValue(newValue);
            const current = cpasTableRef.current;
            return current?.resetTable("ds");
        }
        else {
            const obj = deepFun(treeData, newValue);
            const currentData = treeData.filter((item) => item.id === noteid); //当前表数据
            if (currentData &&
                currentData.length &&
                currentData[0]?.title === obj?.title) {
                message.warning("当前选择的表数据重复,请重新选择~");
            }
            setValue(obj?.title);
            setTreeItem(obj);
            getCpasTableData(obj?.fid, "zl");
        }
    };
    /**
     *
     * @param tids 【 当前选中值 fid 】
     * @param source 【 整理前后 】
     * @param type  【 整理前后 】
     * @returns 根据披露表选中值 获取数据
     */
    const getCpasTableData = async (tids, source, type = "zl") => {
        let params = {
            // dbname: "FI-2017-00001",
            tid: source ? tids + "-" + source : tids,
            type,
            data_type: "json",
            grid_data_type: "display",
        };
        const data = await getCpasTableList(params);
        data.data.tableConfig = JSON.parse(data.data.tableConfig);
        if (data.ok) {
            return setResultData(data.data);
        }
        else {
            return message.error("数据加载失败");
        }
    };
    // 取数源下拉选
    const setSourceData = (e) => {
        if (e) {
            setSourceName(e);
            getCpasTableData(treeItem?.fid, e, e);
        }
        else {
            setSourceName(e);
            if (treeItem.fid) {
                getCpasTableData(treeItem.fid, "", "ds");
            }
        }
    };
    return (_jsxs(React.Fragment, { children: [_jsx("div", { className: "title", children: _jsxs("div", { className: "title-left ", children: [_jsxs("div", { className: "title-years", children: [_jsx("label", { children: " \u5E74\u5EA6: " }), _jsx("div", { children: _jsx(DevSelects, { onValueChanged: (e) => {
                                            setYear(e.value);
                                            initDataVal(e.value);
                                            setValue("");
                                            setTreeItem({});
                                            setSourceName(dataOptions && dataOptions.length
                                                ? dataOptions[0].value
                                                : "");
                                            const current = cpasTableRef.current;
                                            return current?.resetTable("ds");
                                        }, yearVals: year, placeholder: "\u8BF7\u9009\u62E9\u5E74\u5EA6" }) })] }), _jsxs("div", { className: "title-tab", children: [_jsx("label", { style: { marginLeft: "15px" }, children: " \u62AB\u9732\u9879\u76EE: " }), _jsx("div", { className: "tree-sel", children: _jsx(TreeSelect, { showSearch: true, style: { width: "200px" }, value: value, dropdownStyle: {
                                            maxHeight: 600,
                                            overflow: "auto",
                                            minWidth: 300,
                                            background: "#fff",
                                        }, placeholder: "\u8BF7\u9009\u62E9\u62AB\u9732\u9879\u76EE", allowClear: true, popupClassName: "sel-popup", maxTagTextLength: 100000, treeDefaultExpandAll: true, onChange: (e) => onChange(e), treeData: treeData }) }), _jsx("label", { style: { marginLeft: "15px" }, children: " \u53D6\u6570\u6E90: " }), _jsx("div", { children: _jsx(Select, { onChange: (e) => setSourceData(e), defaultValue: "zl", value: source, options: dataOptions, placeholder: "\u8BF7\u9009\u62E9\u53D6\u6570\u6E90", style: { width: "150px" } }) })] })] }) }), _jsx("div", { className: "cpas-tables", children: _jsx(CpasTable, { mapRowsList: [], mapColumnsList: [], tabGroup: "source-tab", numberZeroReplace: " ", tabLevel: 1, isTest: true, model: "read", isShowMenuToolBar: false, gridData: resultData, ref: cpasTableRef, type: parentType, name: "source", dbCellClick: onCellClick }) })] }));
});
// 报表项目
export const TableFS = forwardRef((props, ref) => {
    const { sourceOrCheck, focusItem, dataList, setDataList, gridDatas, parentParamsRef, parentType, } = props;
    const cpasTableFsRef = useRef(null);
    const [getVerifyFormula, setVerifyFormula] = useState([]);
    const [value, setValue] = useState();
    const [resultData, setResultData] = useState(null);
    const [fsTableData, setFsTableData] = useState([]);
    const debouncedApiCall = useCallback(debouncedApi, [dataList]);
    // 单元格双击
    const onCellClick = (item, c, f, name) => {
        if (sourceOrCheck === "source") {
            const dynamicCondition = focusItem && focusItem?.dynamicCondition ? true : false;
            const dynamicVal = dynamicFormulaReplace({
                dynamicCondition,
                dynamicType: "column",
                paramType: "parameter",
                type: "fs",
                col: c.col,
                formulaData: {
                    tname: `${value}`,
                    rowVal: name.rowMap,
                    colVal: name.columnMap,
                    year: "本年",
                },
            });
            const val = dynamicVal
                ? dynamicVal
                : `CP_CELL("${value}","${name.rowMap}","${name.columnMap}")`;
            console.log(dynamicVal, "dynamicVal----");
            getVerifyFormula.push(val);
            setVerifyFormula([...getVerifyFormula]);
            const newVal = focusItem.verifyFormula
                ? focusItem.verifyFormula + "+" + getVerifyFormula.join("+")
                : getVerifyFormula.join("+");
            focusItem.verifyFormula = newVal;
            setVerifyFormula([]);
            setDataList([...dataList]);
            debouncedApiCall(newVal, focusItem, "verifyFormat", "verifyFormula", parentParamsRef, dataList, setDataList);
        }
        else {
            return message.error("请选择数据源");
        }
    };
    const onFsTableChange = async (e) => {
        if (e) {
            const obj = fsTableData.filter((item) => item.tid === e);
            setValue(obj[0].name);
            let params = {
                tid: e,
                type: "fs",
                data_type: "json",
                grid_data_type: "display",
            };
            const data = await getCpasTableList(params);
            data.data.tableConfig = JSON.parse(data.data.tableConfig);
            if (data.ok) {
                return setResultData(data.data);
            }
            else {
                return message.error("数据加载失败");
            }
        }
        else {
            setValue("");
            const current = cpasTableFsRef.current;
            return current?.resetTable("fs");
        }
    };
    useImperativeHandle(ref, () => ({
        valueFun: () => {
            //setValue("");
            const current = cpasTableFsRef.current;
            current.reRender();
            //current?.resetTable("ds");
        },
        cpasTableRef: cpasTableFsRef,
    }));
    const initFsTableData = async () => {
        const fsData = await getAllDesignTableApi("fs");
        if (fsData.ok) {
            const newFsData = fsData.data.map((item) => {
                let obj = {
                    ...item,
                    value: item.tid,
                    label: item.name,
                    title: item.name,
                };
                const name = item.name;
                return obj;
            });
            const resData = sortCpasTable(newFsData.slice(0));
            const filterData = resData.filter((item) => !item.name.includes("试算平衡"));
            console.log(filterData, "filterData-------");
            setFsTableData(filterData);
        }
        else {
            return message.error("数据加载失败");
        }
    };
    useEffect(() => {
        initFsTableData();
    }, []);
    return (_jsxs(React.Fragment, { children: [_jsxs("div", { style: { marginBottom: "20px" }, children: [_jsx("label", { children: "\u62A5\u8868\uFF1A" }), _jsx(Select, { showSearch: true, style: { width: "200px" }, placeholder: "\u8BF7\u9009\u62E9\u9879\u76EE", allowClear: true, dropdownStyle: {
                            maxHeight: 600,
                            overflow: "auto",
                            minWidth: 300,
                            background: "#fff",
                        }, value: value, optionFilterProp: "children", onChange: (e) => onFsTableChange(e), options: fsTableData, filterOption: (input, option) => (option?.label ?? "").toLowerCase().includes(input.toLowerCase()) })] }), _jsx("div", { className: "cpas-tables", children: _jsx(CpasTable, { mapRowsList: [], mapColumnsList: [], tabGroup: "source-tab", tabLevel: 1, isTest: true, model: "read", isShowMenuToolBar: false, gridData: resultData, ref: cpasTableFsRef, name: "source", numberZeroReplace: " ", dbCellClick: onCellClick, type: parentType }) })] }));
});
// 试算报表项目
export const TrialTableFS = forwardRef((props, ref) => {
    const { sourceOrCheck, focusItem, dataList, setDataList, gridDatas, parentParamsRef, parentType, } = props;
    const cpasTableFsRef = useRef(null);
    const [getVerifyFormula, setVerifyFormula] = useState([]);
    const [value, setValue] = useState();
    const [typeValue, setTypeValue] = useState();
    const [resultData, setResultData] = useState(null);
    const [fsTableData, setFsTableData] = useState([]);
    const [fsTypeData, setTypeData] = useState([]);
    const [year, setYear] = useState("本年");
    const [currentDataId, setCurrentDataId] = useState("");
    const [cpasTname, setCpasTname] = useState("");
    const debouncedApiCall = useCallback(debouncedApi, [dataList]);
    // 单元格双击
    const onCellClick = (item, c, f, name) => {
        if (sourceOrCheck === "source") {
            console.log(value, "TypeValue---");
            const dynamicCondition = focusItem && focusItem?.dynamicCondition ? true : false;
            const dynamicVal = dynamicFormulaReplace({
                dynamicCondition,
                dynamicType: "table",
                paramType: "parameter",
                type: "fs",
                col: c.col,
                formulaData: {
                    tname: `${cpasTname}`,
                    rowVal: name.rowMap,
                    colVal: name.columnMap,
                    // year: "本年",
                },
            });
            console.log(dynamicVal, "dynamicVal---");
            const val = dynamicVal
                ? dynamicVal
                : `CP_CELL("${cpasTname}","${name.rowMap}","${name.columnMap}")`;
            getVerifyFormula.push(val);
            setVerifyFormula([...getVerifyFormula]);
            const newVal = focusItem.verifyFormula
                ? focusItem.verifyFormula + "+" + getVerifyFormula.join("+")
                : getVerifyFormula.join("+");
            focusItem.verifyFormula = newVal;
            setVerifyFormula([]);
            setDataList([...dataList]);
            debouncedApiCall(newVal, focusItem, "verifyFormat", "verifyFormula", parentParamsRef, dataList, setDataList);
        }
        else {
            return message.error("请选择数据源");
        }
    };
    const onFsTableChange = async (e) => {
        if (e) {
            console.log(currentDataId, "currentDataId---eee", e);
            await initFsTypeData(e);
            setValue(e);
            const current = cpasTableFsRef.current;
            return current?.resetTable("fs");
        }
        else {
            setValue("");
            setTypeValue("");
            const current = cpasTableFsRef.current;
            return current?.resetTable("fs");
        }
    };
    const fechCpasTableList = async (tname, typeData) => {
        setTypeValue(tname);
        const obj = typeData.filter((item) => item.tName === tname);
        setCpasTname(obj[0].cpasTableName);
        let params = {
            tid: obj[0].tid,
            type: "fs",
            data_type: "json",
            grid_data_type: "display",
        };
        const data = await getCpasTableList(params);
        data.data.tableConfig = JSON.parse(data.data.tableConfig);
        if (data.ok) {
            return setResultData(data.data);
        }
        else {
            return message.error("数据加载失败");
        }
    };
    const onFsTableTypeChange = async (e) => {
        if (e) {
            console.log(e, "e------onFsTableTypeChange", fsTypeData);
            fechCpasTableList(e, fsTypeData);
            // setTypeValue(e);
            // const obj = fsTypeData.filter((item) => item.tName === e);
            // let params: any = {
            //   tid: obj[0].tid,
            //   type: "fs",
            //   data_type: "json",
            //   grid_data_type: "display",
            // };
            // const data = await getCpasTableList(params);
            // data.data.tableConfig = JSON.parse(data.data.tableConfig);
            // if (data.ok) {
            //   return setResultData(data.data);
            // } else {
            //   return message.error("数据加载失败");
            // }
        }
        else {
            setValue("");
            setTypeValue("");
            const current = cpasTableFsRef.current;
            return current?.resetTable("fs");
        }
    };
    useImperativeHandle(ref, () => ({
        valueFun: () => {
            //setValue("");
            const current = cpasTableFsRef.current;
            current.reRender();
            //current?.resetTable("ds");
        },
        cpasTableRef: cpasTableFsRef,
    }));
    const initDataVal = async (years) => {
        const data = await getYears(years);
        setCurrentDataId(data);
        await initFsTableData(data);
    };
    const initFsTableData = async (data_id) => {
        const dataId = data_id ? data_id : await getDataId();
        setCurrentDataId(dataId);
        const fsData = await getCheckTypeApi("account.ssph.table", {
            year: dataId,
        });
        if (fsData && fsData.length) {
            const newFsData = fsData.map((item) => {
                let obj = {
                    ...item,
                    value: item.tName,
                    label: item.tName,
                };
                return obj;
            });
            setFsTableData(newFsData);
        }
        else {
            setFsTableData([]);
        }
    };
    const initFsTypeData = async (tName) => {
        const fsTypeData = await getCheckTypeApi("account.ssph.tableType", {
            year: currentDataId,
            tName: tName,
        });
        if (fsTypeData && fsTypeData.length) {
            const newFsData = fsTypeData.map((item) => {
                let obj = {
                    ...item,
                    value: item.tName,
                    label: item.tName,
                };
                return obj;
            });
            setTypeData(newFsData);
            if (newFsData && newFsData.length === 1) {
                setTypeValue(newFsData[0].value);
                fechCpasTableList(newFsData[0].value, newFsData);
            }
            else {
                setTypeValue("");
            }
        }
        else {
            setTypeData([]);
            setTypeValue("");
        }
    };
    useEffect(() => {
        initFsTableData();
    }, []);
    return (_jsxs(React.Fragment, { children: [_jsx("div", { className: "title", children: _jsxs("div", { className: "title-left ", children: [_jsxs("div", { className: "title-years", children: [_jsx("label", { children: " \u5E74\u5EA6: " }), _jsx("div", { children: _jsx(DevSelects, { onValueChanged: (e) => {
                                            setYear(e.value);
                                            initDataVal(e.value);
                                            setValue("");
                                            setTypeValue("");
                                            const current = cpasTableFsRef.current;
                                            return current?.resetTable("ds");
                                        }, yearVals: year, placeholder: "\u8BF7\u9009\u62E9\u5E74\u5EA6" }) })] }), _jsxs("div", { className: "title-tab", style: { paddingLeft: 20 }, children: [_jsx("label", { children: "\u62A5\u8868\uFF1A" }), _jsx(Select, { showSearch: true, style: { width: "180px" }, placeholder: "\u8BF7\u9009\u62E9\u9879\u76EE", allowClear: true, dropdownStyle: {
                                        maxHeight: 600,
                                        overflow: "auto",
                                        minWidth: 300,
                                        background: "#fff",
                                    }, value: value, optionFilterProp: "children", onChange: (e) => onFsTableChange(e), options: fsTableData, filterOption: (input, option) => (option?.label ?? "")
                                        .toLowerCase()
                                        .includes(input.toLowerCase()) })] }), _jsxs("div", { className: "title-tab", style: { paddingLeft: 20 }, children: [_jsx("label", { children: "\u671F\u95F4\uFF1A" }), _jsx(Select, { showSearch: true, style: { width: "200px" }, placeholder: "\u8BF7\u9009\u62E9", allowClear: true, dropdownStyle: {
                                        maxHeight: 600,
                                        overflow: "auto",
                                        minWidth: 100,
                                        background: "#fff",
                                    }, value: typeValue, optionFilterProp: "children", onChange: (e) => onFsTableTypeChange(e), options: fsTypeData, filterOption: (input, option) => (option?.label ?? "")
                                        .toLowerCase()
                                        .includes(input.toLowerCase()) })] })] }) }), _jsx("div", { className: "cpas-tables", children: _jsx(CpasTable, { mapRowsList: [], mapColumnsList: [], tabGroup: "source-tab", tabLevel: 1, isTest: true, model: "read", isShowMenuToolBar: false, gridData: resultData, ref: cpasTableFsRef, type: parentType, name: "source", numberZeroReplace: " ", dbCellClick: onCellClick }) })] }));
});
